package com.wms.service;

import com.mes.entity.User;
import com.yy.base.common.dao.Dao;
import com.yy.base.common.service.BaseService;
import com.yy.base.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

@Service
public class CPInWarehouseService extends BaseService {
	@Autowired
	private Dao dao;
	private String uuid = null;

	/**
	 * 包装盒条码标签信息
	 * @param id
	 * @return
	 */
	public List<Map<String, Object>> getBoxList(String id) {
		String sql = "";
		return dao.query(sql, id);
	}
	/**
	 * 历史入库信息
	 * @return
	 */
	public List<Map<String, Object>> getHistoryList() {
		String sql = "SELECT  " +
				"  job.CODE AS JOBCODE,  " +
				"  p.NAME AS WLNAME,  " +
				"  ys.NAME AS YSNAME,  " +
				"  cm.code AS CMCODE,  " +
				"  yh.NAME AS YHNAME,  " +
				"  to_char ( sb.created, 'yyyy-mm-dd hh24:mi:ss' ) AS INTIMES,  " +
				"  NULL AS BOXCODE,  " +
				"  NULL AS WULIUCODE   " +
				"FROM  " +
				"  wms_storage_in_out io,  " +
				"  Emeso_job_order job,  " +
				"  m_product p,  " +
				"  ad_user yh ,  " +
				"  Wms_Storage_Batch sb  " +
				"  left join emes_color ys on  sb.emes_color_id = ys.emes_color_id   " +
				"  left join emes_size cm on sb.emes_size_id = cm.emes_size_id   " +
				"WHERE  " +
				"  sb.wms_storage_batch_id = io.wms_storage_batch_id   " +
				"  AND sb.m_product_id = p.m_product_id   " +
				"  AND sb.createdby = yh.ad_user_id   " +
				"  AND io.data_id = job.emeso_job_order_id   " +
				"  AND io.Opt_Mode = '自制件入库'   " +
				"  AND to_char ( sb.created, 'yyyy-mm-dd' ) = to_char ( now( ), 'yyyy-mm-dd' ) ";
		return dao.query(sql);
	}
	//查询工单状态及工单信息
	public List<Map<String, Object>> getjobStatusList(String rfid) {
		String sql = "SELECT  " +
				"  zy.CODE AS ZYCODE,  " +
				"  zy.emeso_job_order_id as JOBID, " +
                "  zygx.Emeso_Job_Order_Exe_id as EXEID , " +
				"  zy.STATUS AS ZYSTATUS,  " +
				"  ( SELECT a.STATUS FROM Emeso_Job_Order_Exe a WHERE a.route_no = zygx.route_no - 10 AND a.emeso_job_order_id = zy.Emeso_Job_Order_id ) AS ZYGXSTATUS,  " +
				"  WL.  " +
				"VALUE  " +
				"  || ' - ' || WL.NAME PRODUCT,  " +
				" wl.m_product_id as WLID," +
				"  ys.NAME AS YSNAME,  " +
				"  ys.emes_color_id AS YSID,  " +
				"  cm.CODE AS CMCODE1,  " +
				"  cm.emes_size_id AS CMID,  " +
				"  sc.c_uom_id AS DWID,  " +
				"  to_char ( xs.Sales_Date, 'yyyy-mm-dd hh24:mi:ss' ) TIMES,  " +
				"  sc.foot_size_code AS CMCODE,  " +
				"  xs.emes_customer_id AS YHNAME,  " +
				"  xs.address AS DIZHI   " +
				"FROM  " +
				"  emeso_job_order zy,  " +
				"  emeso_job_order_exe zygx,  " +
				"  emes_process gx,  " +
				"  m_product wl,  " +
				"  emeso_manuorder sc,  " +
				"  wms_sales_order_detail mx,  " +
				"  wms_sales_order xs,  " +
				"  emesp_planorder jh  " +
				"  LEFT JOIN emes_color ys ON jh.emes_color_id = ys.emes_color_id  " +
				"  LEFT JOIN emes_size cm ON jh.emes_size_id = cm.emes_size_id   " +
				"WHERE  " +
				"  zy.emeso_manuorder_id = sc.emeso_manuorder_id   " +
				"  AND sc.emesp_planorder_id = jh.emesp_planorder_id   " +
				"  AND zy.emeso_job_order_id = zygx.emeso_job_order_id   " +
				"  AND jh.wms_sales_order_detail_id = mx.wms_sales_order_detail_id   " +
				"  AND mx.wms_sales_order_id = xs.wms_sales_order_id   " +
				"  AND zy.m_product_id = wl.m_product_id   " +
				"  AND zygx.emes_process_id = gx.emes_process_id   " +
				"  AND gx.NAME = '包装入库' " +
				"  AND zy.CODE = '"+ rfid +"'";
		return dao.query(sql);
	}

/**
	 * 往批次表中插入成品信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public boolean inStorageBatch(String size, String color, String uomid, String pid, User user) {
		boolean result = false;
		uuid = StringUtil.getUUID();

		String sql = "insert into Wms_Storage_Batch(Wms_Storage_Batch_id,Emes_Size_Id,Emes_Color_ID,Status,C_Uom_ID,In_time,Is_Self,M_Product_ID,Batch_No," +
				"AD_CLIENT_ID, AD_ORG_ID,ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY)" +
				" values(?,?,?,'正常',?,now(),'Y',?,?,?,?,?,now(),?,now(),?)";

		result = dao.executeSql(sql, uuid,size, color, uomid, pid, "", user.getClientId(), user.getOrgId(), "Y", user.getId(), user.getId()) > 0;

		return result;
	}

	/**
	 * 往inout表中插入成品信息
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public boolean inStorageInOut(String warehouseID,String locatorID, int qty, String uomID,String jobid, User user,String jobcode) {
		boolean result = false;
		String uuids = StringUtil.getUUID();

		String sql = "insert into wms_storage_in_out "
				+ " (Wms_Storage_In_Out_ID,Wms_Storage_Batch_ID,M_Warehouse_ID,M_Locator_ID,Qty,C_Uom_ID,Opt_Mode,Opt_Code,Data_Id,Status,"
				+ "AD_CLIENT_ID, AD_ORG_ID,ISACTIVE, CREATED, CREATEDBY, UPDATED, UPDATEDBY)" +
				" values(?,?,?,?,?,?,?,?,?,'OK',?,?,?,now(),?,now(),?)";


		result =  dao.executeSql(sql, uuids,uuid,warehouseID, locatorID,qty,uomID, "自制件入库",jobcode,jobid, user.getClientId(), user.getOrgId(), "Y", user.getId(), user.getId()) > 0;

		return result;
	}

	/**
	 * 修改Emeso_job_order表状态(代表该作业工单已完工入库)
	 * @return
	 */
	@SuppressWarnings("unchecked")
	public boolean uodateStatus(String id) {
		boolean result = false;


		String updatesql = "UPDATE Emeso_job_order SET status = 'FINISHED' WHERE Emeso_job_order_id = ?";

		result = dao.executeSql(updatesql,  id ) > 0;


		return result;
	}


	//修改工序的执行状态开工
	@SuppressWarnings("unchecked")
	public boolean UpdateExeBeginStatus(String Ids,  String status) {
		boolean result = false;


		String sql =  "UPDATE Emeso_Job_Order_Exe SET status = ?, Exe_Date_From = now()  WHERE  Emeso_Job_Order_Exe_id = ? ";

		result = dao.executeSql(sql, status,Ids) > 0;

		return result;
	}

	//修改工序的执行状态为完工
	@SuppressWarnings("unchecked")
	public boolean UpdateExeEndStatus(String Ids ,String status) {
		boolean result = false;


		String sql =  "UPDATE Emeso_Job_Order_Exe SET status = ?, Exe_Date_To =  now()  WHERE  Emeso_Job_Order_Exe_id = ? ";

		result = dao.executeSql(sql, status,Ids) > 0;

		return result;
	}


}